﻿namespace ConsoleApp6
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 定义题目及其概率
            Dictionary<string, double> probabilities = new Dictionary<string, double>
        {
            { "A", 0.15 },
            { "B", 0.35 },
            { "C", 0.15 },
            { "D", 0.35 }
        };

            // 模拟50个学员抽取两个题目
            List<string> strings = new List<string>();
            for (int i = 1; i <= 50; i++)
            {
                Console.WriteLine($"学员{i}抽取的题目:");
                List<string> drawnQuestions = DrawTwoQuestions(probabilities);
                string s=string.Empty;
                foreach (string question in drawnQuestions)
                {
                    s+=question;
                    Console.WriteLine(question);
                }
                strings.Add(s);
          
                Console.WriteLine();
            }
            Console.WriteLine(strings.Where(a => a.Contains("A")).ToList().Count());
            Console.WriteLine(strings.Where(a => a.Contains("B")).ToList().Count());
            Console.WriteLine(strings.Where(a => a.Contains("C")).ToList().Count());
            Console.WriteLine(strings.Where(a => a.Contains("D")).ToList().Count());
        }

        static List<string> DrawTwoQuestions(Dictionary<string, double> probabilities)
        {
            List<string> questions = new List<string>(probabilities.Keys);
            List<string> drawnQuestions = new List<string>();

            // 根据概率抽取两个不同的题目
            while (drawnQuestions.Count < 2)
            {
                double randomValue = GetRandomValueBasedOnProbabilities(probabilities);
                string selectedQuestion = GetQuestionByProbability(probabilities, randomValue);

                if (!drawnQuestions.Contains(selectedQuestion))
                {
                    drawnQuestions.Add(selectedQuestion);
                }
            }

            return drawnQuestions;
        }

        static double GetRandomValueBasedOnProbabilities(Dictionary<string, double> probabilities)
        {
            double totalProbability = 0.0;
            foreach (var probability in probabilities.Values)
            {
                totalProbability += probability;
            }

            return new Random().NextDouble() * totalProbability;
        }

        static string GetQuestionByProbability(Dictionary<string, double> probabilities, double randomValue)
        {
            double cumulativeProbability = 0.0;
            foreach (var question in probabilities.Keys)
            {
                cumulativeProbability += probabilities[question];
                if (randomValue <= cumulativeProbability)
                {
                    return question;
                }
            }

            return null;
        }
    }
}
